import {
  HttpException,
  HttpStatus,
  ArgumentsHost,
  Catch,
  ExceptionFilter,
} from '@nestjs/common';
import CommonUtil from '../tool/CommonUtil';

//用于抛出业务异常，业务异常不视为服务器异常
export class BusinessException extends HttpException {
  constructor(msg = '服务异常') {
    super(msg, HttpStatus.OK);
  }
}

@Catch(BusinessException)
export class BusinessExceptionsFilter implements ExceptionFilter {
  catch(exception: BusinessException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();

    response.status(200).json(CommonUtil.returnError(exception.message));
  }
}
